<?php

namespace app\v1\model;

use think\Model;
use think\Db;

/**
 * Class Leave
 * @package app\v1\model
 * 模块说明：这个类是用来处理请假的数据
 */

class Leave extends Base
{
    //提交请假
    public function commitLeave($startDate,$endDate){
            $this->checkDate($startDate,$endDate);
            if($startDate==$endDate){
                $rs=$this->checkEnroll($startDate);
                if(empty($rs)){
                   renderInfo(400001,'该日期没有报名课程,请重新选择请假日期');
                }
                $rs2=$this->checkHasLeave($startDate);
                if(!empty($rs2)){
                    renderInfo(400001,'该日期已经请假,请重新选择日期');
                }
                Db::startTrans();
                try {
                    foreach ($rs as $key => $value) {
                        $res = $this->addLeave($startDate, $value['id'], $value['typeid']);
                    }
                    Db::commit();
                    renderInfo(0,'操作成功');
                }catch (\Exception $e){
                    Db::rollback();
                    renderInfo(400008,'系统繁忙,请稍后重试');
                }
            }else{
                Db::startTrans();
                try{
                    //$startD=(int)date('d',$startDate);
                    //$en
                    $dateArray=loopDate($startDate,$endDate);
                    $enrollArray=array();
                    foreach($dateArray as $key=>$value){
                        $theResList=$this->checkEnroll($value);
                        $theRes2=$this->checkHasLeave($value);
                        foreach($theResList as $enkey=>$envalue){
                            $enrollArray[$key][$enkey]['enrollId']=$envalue['id'];
                            $enrollArray[$key][$enkey]['type']=$envalue['typeid'];
                        }
                        if(empty($theResList)){
                            renderInfo(400001,$value.'没有报名课程,请重新选择日期');
                        }
                        if(!empty($theRes2)){
                            renderInfo(400001,$value.'已经请假,请重新选择日期');
                        }
                    }

                    foreach($dateArray as $key=>$value){
                        foreach($enrollArray[$key] as $enkey=>$envalue){
                            $this->addLeave($value,$envalue['enrollId'],$envalue['type']);
                        }
                    }

                    // 提交事务
                    Db::commit();
                    renderInfo(0,'请假成功');
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    renderInfo(400008,'系统繁忙,请稍后重试');
                }

            }

    }

    //检查签到表里面是否已经有某个日期的请假信息
    public function checkHasLeave($date){
        $userInfo=getUserInfo();
        $map['_orgid']=$userInfo['orgId'];
        $map['_shopid']=$userInfo['schoolId'];
        $map['_studentid']=$userInfo['childId'];
        $map['date']=$date;
        $res=Db::name('childcare_sign')->where($map)->find();
        if(!empty($res)){
            return true;
        }else{
            return false;
        }
    }

    //往数据库插入请假信息
    public function addLeave($date,$enrollId,$type){
        $userInfo=getUserInfo();
        $data['appenduserid']=$userInfo['parentId'];$data['appendtime']=time();
        $data['modifyuserid']=$userInfo['parentId'];
        $data['modifytime']=time();
        $data['effect']=1;
        $data['active']=1;
        $data['enrollid']=$enrollId;
        $data['_orgid']=$userInfo['orgId'];
        $data['_shopid']=$userInfo['schoolId'];
        $data['_studentid']=$userInfo['childId'];
        $data['_typeid']=$type;
        $data['date']=$date;
        $data['leave']=1;
      //  die;
        $res=Db::name('childcare_sign')->insert($data);
//        echo Db::getLastSql();
//        die;
        return $res;
    }

    //检查某天是否有报名课程(包含午托晚托)
    public function checkEnroll($date){
        $userInfo=getUserInfo();
        $map['studentid']=$userInfo['childId'];
        $res=Db::name('childcare_enroll')->where('startdate<='."'".$date."'".' and enddate>='."'".$date."'")->where($map)->select();
//        echo Db::getLastSql();
//        exit;
        return $res;
    }

    //判断请假日期是否符合逻辑
    public function checkDate($startDate,$endDate){
        $today=strtotime(date('Y-m-d'));
        $startTime=strtotime($startDate);
        $endTime=strtotime($endDate);
        if($startTime>$endTime||$startTime<$today ){
            renderInfo(400010,'选择时间段不符合要求');
        }
    }

    //获取孩子未过期可以取消的请假列表
    public function getLeaveList(){
        $userInfo=getUserInfo();
        $today=date('Y-m-d');
        $map['_orgid']=$userInfo['orgId'];
        $map['_shopid']=$userInfo['schoolId'];
        $map['_studentid']=$userInfo['childId'];
        $map['leave']=1;
        $leaveList=Db::name('childcare_sign')->where($map)->where('date>='."'".$today."'")->field('id as leaveId,case _typeid when 3 then 2 else _typeid end as type,date')->distinct(true)->select();
        renderData(0,'ok',$leaveList);
    }
    //取消请假
    public function cancelLeave($leaveIds){
        $leaveArray=explode(',',$leaveIds);
        //验证请假列表Id是否合法
        foreach($leaveArray as $key=>$value){
            $res=$this->checkLeave($value);
            if(!$res){
                renderInfo(400009,'不合法参数');
            }
        }
        Db::startTrans();
        try{


               // Db::name('childcare_sign')->where()->delete();
           // $data=array('effect'=>0,'active'=>0);
            Db::name('childcare_sign')->where('id','in',$leaveArray)->delete();
            Db::commit();
            renderInfo(0,'取消成功');
        }catch (\Exception $e){
            Db::rollback();
            renderInfo(400008,'系统繁忙,请稍后重试');
        }


    }
    //验证请假列表Id的正确性
    public function checkLeave($leaveId){
        $userInfo=getUserInfo();
        $today=date('Y-m-d');
        $map['_orgid']=$userInfo['orgId'];
        $map['_shopid']=$userInfo['schoolId'];
        $map['_studentid']=$userInfo['childId'];
        $map['leave']=1;
        $map['id']=$leaveId;
        $leaveInfo=Db::name('childcare_sign')->where($map)->where('date>='."'".$today."'")->find();
        if(!empty($leaveInfo)){
            return true;
        }else{
            return false;
        }

    }


}
